import 'package:flutter/material.dart';
import 'package:mobile_app.core/core/presentation/theme/styles.dart';

part 'styles.dart';

part 'text_theme.dart';

class SigningInPageTheme extends InheritedTheme {
  SigningInPageTheme({
    required Brightness brightness,
    required Color primaryContrastingColor,
    required super.child,
    super.key,
  })  : _brightness = brightness,
        _primaryContrastingColor = primaryContrastingColor,
        _data = _buildThemeData(
          brightness: brightness,
          primaryContrastingColor: primaryContrastingColor,
        );

  final Brightness _brightness;
  final Color _primaryContrastingColor;
  final SigningInPageThemeData _data;

  static SigningInPageThemeData _buildThemeData({
    required Brightness brightness,
    required Color primaryContrastingColor,
  }) {
    return switch (brightness) {
      Brightness.dark => SigningInPageThemeData.dark(
        primaryContrastingColor: primaryContrastingColor,
      ),
      Brightness.light => SigningInPageThemeData.light(
        primaryContrastingColor: primaryContrastingColor,
      ),
    };
  }

  static SigningInPageThemeData of(BuildContext context) {
    final inheritedTheme = context.dependOnInheritedWidgetOfExactType<SigningInPageTheme>()!;
    return inheritedTheme._data;
  }

  @override
  Widget wrap(BuildContext context, Widget child) {
    final ancestorTheme = context.findAncestorWidgetOfExactType<SigningInPageTheme>();

    if (identical(this, ancestorTheme)) {
      return child;
    }

    return SigningInPageTheme(
      brightness: _brightness,
      primaryContrastingColor: _primaryContrastingColor,
      child: child,
    );
  }

  @override
  bool updateShouldNotify(SigningInPageTheme oldWidget) {
    return _brightness != oldWidget._brightness ||
        _primaryContrastingColor != oldWidget._primaryContrastingColor;
  }
}

class SigningInPageThemeData {
  const SigningInPageThemeData._internal({
    required this.unfocusedTextFieldBorderColor,
    required this.textFieldBackgroundColor,
    required this.pageClosingIconColor,
    required this.backgroundColor,
    required this.textTheme,
  });

  factory SigningInPageThemeData.light({
    required Color primaryContrastingColor,
  }) {
    return SigningInPageThemeData._internal(
      unfocusedTextFieldBorderColor: CoreStyles.grey1,
      textFieldBackgroundColor: Colors.white,
      pageClosingIconColor: CoreStyles.grey4,
      backgroundColor: Colors.white,
      textTheme: SigningInPageTextTheme.light(
        primaryContrastingColor: primaryContrastingColor,
      ),
    );
  }

  factory SigningInPageThemeData.dark({
    required Color primaryContrastingColor,
  }) {
    return SigningInPageThemeData._internal(
      unfocusedTextFieldBorderColor: CoreStyles.grey5,
      textFieldBackgroundColor: CoreStyles.darkGrey4,
      pageClosingIconColor: CoreStyles.grey2,
      backgroundColor: CoreStyles.darkGrey4,
      textTheme: SigningInPageTextTheme.dark(
        primaryContrastingColor: primaryContrastingColor,
      ),
    );
  }

  final Color unfocusedTextFieldBorderColor;
  final Color textFieldBackgroundColor;
  final Color pageClosingIconColor;
  final Color backgroundColor;
  final SigningInPageTextTheme textTheme;
}
